// 当然可以！这里有一个简单的JavaScript游戏示例，我们将创建一个猜数字游戏。在这个游戏中，程序随机生成一个1到10之间的数字，玩家需要猜测这个数字是什么。

// 设置游戏参数
const maxNumber = 10;
let randomNumber = Math.floor(Math.random() * maxNumber) + 1; // 随机生成1到10之间的数字
let guesses = document.querySelector('.guesses');
let lastResult = document.querySelector('.lastResult');
let lowOrHi = document.querySelector('.lowOrHi');

// 显示游戏说明
document.querySelector('.welcome').style.display = 'block';

// 游戏逻辑
function checkGuess() {
    const userGuess = Number(document.querySelector('.userGuess').value); // 获取用户输入的数字

    // 检查用户输入是否为空
    if (userGuess < 1 || userGuess > maxNumber) {
        lastResult.textContent = 'Please enter a number between 1 and ' + maxNumber;
    } else {
        // 更新猜测次数
        guesses.textContent = 'Guesses so far: ' + userGuess;

        if (userGuess === randomNumber) {
            lastResult.textContent = 'Congratulations! You got it right!';
            lastResult.style.backgroundColor = 'green';
            lowOrHi.textContent = '';
            setGameOver();
        } else if (userGuess < randomNumber) {
            lastResult.textContent = 'Last guess was too low!';
            lowOrHi.textContent = 'Last guess: ' + userGuess;
        } else if (userGuess > randomNumber) {
            lastResult.textContent = 'Last guess was too high!';
            lowOrHi.textContent = 'Last guess: ' + userGuess;
        }
    }
}

// 设置游戏结束逻辑
function setGameOver() {
    const resetButton = document.createElement('button');
    resetButton.textContent = 'Start new game';
    document.body.appendChild(resetButton);
    resetButton.addEventListener('click', resetGame);
}

// 重置游戏
function resetGame() {
    document.querySelector('.welcome').style.display = 'block';
    document.querySelector('.game').style.display = 'none';
    resetButton.parentNode.removeChild(resetButton);
}

// 显示游戏界面
document.querySelector('.startBtn').addEventListener('click', function() {
    document.querySelector('.welcome').style.display = 'none';
    document.querySelector('.game').style.display = 'block';
});

// 绑定输入框和按钮事件
document.querySelector('.guessSubmit').addEventListener('click', checkGuess);
